#mainTextBox Code generation is a powerful feature of NeuroSolutions (Professional version and above) that generates ANSI-compatible C++ code for any network that the user can build. The code then can be compiled in conjunction with the NeuroSolutions object library to produce the core neural network. The two most common uses of code generation are: 1) embedding neural network code in another application, usually in recall or testing mode on an already trained network, and 2) speeding up training of large networks and/or data sets by using a faster platform. We will consider both of these cases using an MLP and the exclusive-or data set.
#subtitleTextBox Embedded testing
#mainTextBox In many cases the user will train a network in NeuroSolutions and then use Code Generation to perform testing (recall) within some other application and/or platform. The first step is to train the network. Hit the run button to train for 100 epochs and then step through the 4 exemplars to make sure the network has learned. Next, save the weights and view them. These saved weights will be used by the compiled executable programs in the next two panels.
#subtitleTextBox Testing the compiled program with a file
#mainTextBox We have removed all backprop-related components and all probes, except the output probe. The "Code Generation File Format" for the File component has been set to "Binary." When C++ code is generated, a default binary input file is automatically created from the input file. You can, however, specify any file you want prior to compilation. We have set the "Code Generation File Format" for the output probe to "Stdio," which will send the output to the console. Both the source code and executable, which we have pre-compiled, are generated from the StaticControl component. Run the executable and verify that it agrees with the breadboard network. View the source code.
#subtitleTextBox Testing the compiled program from the console
#mainTextBox For this test, instead of reading the input data from a file, the user will input it from the keyboard, one exemplar at a time. To do this, the "Code Generation File Format" for the File component was set to "Function," which creates a blank function in the source code that the user must finish. Here we have added code to query the user for two data points at a time. Run the pre-compiled executable and enter in "-1 -1" (without the quotes) to verify that it agrees with the original network. You can input any data pair, but remember that for comparison purposes the four training input pairs were (-1 -1), (-1 1), (1 -1), and (1 1).
#subtitleTextBox Embedded training
#mainTextBox Instead of training in NeuroSolutions and testing in the compiled program, this time we'll take the opposite approach. We will train in the compiled program, which will automatically save the weights after the last epoch. Then, we can load the weights into the NeuroSolutions breadboard network for testing. Run the compiled program to train the network. It will display the cost at each epoch. View the source weights. Repeat the training, if you wish, to verify that the weights change. Then, view the source code, which was generated from the network below, and compare some of the values in the source code to the breadboard network using the inspector.
#subtitleTextBox Testing within NeuroSolutions
#mainTextBox We have loaded in the weights from the previous training. You can verify this by viewing the matrix editors on the synapses and comparing them to the weight file. Single-step through the four exemplars to verify that, indeed, these weights solve the x-or problem.
#subtitleTextBox Summary
#mainTextBox You have seen how NeuroSolutions gives you tremendous options for generating C++ code. Whether embedding code in other applications or training on faster platforms, the process is simple because NeuroSolutions generates the code from the breadboard that you build.